package com.devbobcorn.nekoration.items;

import com.devbobcorn.nekoration.NekoColors.EnumNekoColor;
import com.devbobcorn.nekoration.NekoColors.EnumWoodenColor;

import net.minecraft.util.collection.DefaultedList;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.block.Block;

public class HalfTimberBlockItem extends BlockItem {
    public static final String COLOR_0 = "color_0"; // Color of outer layer
    public static final String COLOR_1 = "color_1"; // Color of inner layer

    public HalfTimberBlockItem(Block block, Settings settings) {
        super(block, settings);
    }

    @Override
    public void appendStacks(ItemGroup tab, DefaultedList<ItemStack> subItems) {
        // Only add those with
        if (this.isIn(tab)) {
            for (EnumNekoColor color : EnumNekoColor.values()) {
                ItemStack subItemStack = new ItemStack(this, 1);
                setColor0(subItemStack, EnumWoodenColor.BROWN);
                setColor1(subItemStack, color);
                subItems.add(subItemStack);
            }
        }
    }

    public void fillItemCategoryWithWoodType(ItemGroup tab, EnumWoodenColor type, DefaultedList<ItemStack> subItems) {
        if (this.isIn(tab)) {
            for (EnumNekoColor color : EnumNekoColor.values()){
                ItemStack subItemStack = new ItemStack(this, 1);
                setColor0(subItemStack, type);
                setColor1(subItemStack, color);
                subItems.add(subItemStack);
            }
        }
    }

    public static boolean hasColor(ItemStack stack) {
        return stack.getOrCreateNbt().contains(COLOR_0) && stack.getOrCreateNbt().contains(COLOR_1);
    }

    public static EnumWoodenColor getColor0(ItemStack stack) {
        NbtCompound tag = stack.getOrCreateNbt();
        return EnumWoodenColor.fromNBT(tag, COLOR_0);
    }

    public static void setColor0(ItemStack stack, EnumWoodenColor color) {
        NbtCompound tag = stack.getOrCreateNbt();
        color.putIntoNBT(tag, COLOR_0);
    }

    public static EnumNekoColor getColor1(ItemStack stack) {
        NbtCompound tag = stack.getOrCreateNbt();
        return EnumNekoColor.fromNBT(tag, COLOR_1);
    }

    public static void setColor1(ItemStack stack, EnumNekoColor color) {
        NbtCompound tag = stack.getOrCreateNbt();
        color.putIntoNBT(tag, COLOR_1);
    }

    @Override
    public Text getName(ItemStack stack) {
        boolean hasColor = hasColor(stack);
        String color0Text = (new TranslatableText("color.wooden." + (hasColor ? getColor0(stack).asString() : "unknown"))).getString();
        String color1Text = (new TranslatableText("color.nekoration." + (hasColor ? getColor1(stack).asString() : "unknown"))).getString();

        return CaseTweak.getTweaked(new TranslatableText(this.getTranslationKey(stack), color0Text, color1Text));
    }
}
